var mobId, mob, stats;
var 清怪 = true;
var 刷新地图 = true;
var 复活次数 = 11;
var 副本名称 = "组队任务_女神塔";
var BOSS_1_id = 9300039;
var BOSS_1_血量倍率 = 60;
var 副本时限 = 1000 * 60 * 40;
var 实例地图 = [920010000, 920010100, 920010200, 920010300, 920010400, 920010900, 920010910, 920010912, 920010920, 920010922, 920010930, 920010932, 920010700, 920010800, 920011100];
var 准备地图 = 933030000;
var 退场倒计时 = 40;
var stg2_combo0 = ["3", "2", "1"];
var stg2_combo1 = ["0", "0", "1"];
var stg2_combo2 = ["0", "1", "1"];
var stg6_combo = ["00", "01", "02", "03", "04", "05", "06", "07", "08"];
var cx = [200, -300, -300, -300, 200, 200, 200, -300, -300, 200, 200, -300, -300, 200];
var cy = [-2321, -2114, -2910, -2510, -1526, -2716, -717, -1310, -3357, -1912, -1122, -1736, -915, -3116];

function init() {}

function setup(a, h) {
    em.getProperties().clear();
    em.setProperty("state", "1");
    em.setProperty("leader", "true");
    var g = em.newInstance(副本名称);
    em.setProperty("stage", "0");
    em.setProperty("pre", "0");
    em.setProperty("finished", "0");
    em.setProperty("stage2", "0");
    em.setProperty("stage3", "0");
    em.setProperty("stage4", "0");
    em.setProperty("feather_end", "0");
    var r = java.lang.Math.floor(java.lang.Math.random() * stg2_combo0.length);
    var s = java.lang.Math.random();
    var q = s < 0.33 ? true : false;
    var n = s < 0.66 ? true : false;
    em.setProperty("stage4_0", q ? stg2_combo0[r] : (n ? stg2_combo1[r] : stg2_combo2[r]));
    em.setProperty("stage4_1", q ? stg2_combo1[r] : (n ? stg2_combo2[r] : stg2_combo0[r]));
    em.setProperty("stage4_2", q ? stg2_combo2[r] : (n ? stg2_combo0[r] : stg2_combo1[r]));
    em.setProperty("stage6", "0");
    for (var t = 0; t < stg6_combo.length; t++) {
        for (var k = 0; k < 4; k++) {
            em.setProperty("stage6_" + stg6_combo[t] + "" + (k + 1) + "", "0")
        }
    }
    for (var t = 0; t < stg6_combo.length; t++) {
        var m = false;
        while (!m) {
            for (var l = 0; l < 4; l++) {
                if (!m) {
                    var d = false;
                    for (var j = 0; j < 4; j++) {
                        if (em.getProperty("stage6_" + stg6_combo[t] + "" + (j + 1) + "") == null) {
                            em.setProperty("stage6_" + stg6_combo[t] + "" + (j + 1) + "", "0")
                        } else {
                            if (em.getProperty("stage6_" + stg6_combo[t] + "" + (j + 1) + "").equals("1")) {
                                d = true;
                                break
                            }
                        }
                    }
                    if (!d && java.lang.Math.random() < 0.25) {
                        em.setProperty("stage6_" + stg6_combo[t] + "" + (l + 1) + "", "1");
                        m = true;
                        break
                    }
                }
            }
        }
    }
    for (var o = 0; o < 3; o++) {
        em.setProperty("stage62_" + o + "", "0")
    }
    var f = false;
    while (!f) {
        for (var o = 0; o < 3; o++) {
            if (em.getProperty("stage62_" + o + "") == null) {
                em.setProperty("stage62_" + o + "", "0")
            } else {
                if (!f && java.lang.Math.random() < 0.2) {
                    em.setProperty("stage62_" + o + "", "1");
                    f = true
                }
            }
        }
    }
    var e = false;
    while (!e) {
        for (var o = 0; o < 3; o++) {
            if (em.getProperty("stage62_" + o + "") == null) {
                em.setProperty("stage62_" + o + "", "0")
            } else {
                if (!em.getProperty("stage62_" + o + "").equals("1") && !e && java.lang.Math.random() < 0.2) {
                    em.setProperty("stage62_" + o + "", "1");
                    e = true
                }
            }
        }
    }
    em.setProperty("stage7", "0");
    em.setProperty("done", "0");
    for (var o = 0; o < 实例地图.length; o++) {
        var u = g.setInstanceMap(实例地图[o]);
        u.resetPQ(a);
        u.resetFully();
        u.killAllMonsters(清怪);
        u.respawn(刷新地图)
    }
    g.startEventTimer(副本时限);
    g.setProperty("FBtime", 副本时限);
    g.setProperty("FBname", 副本名称);
    g.setProperty("strMap", 准备地图);
    var c = em.getMonster(BOSS_1_id).getMobMaxHp() * BOSS_1_血量倍率;
    g.setProperty("BOSShp", c);
    em.setProperty("stage6", "0");
    for (var t = 0; t < stg6_combo.length; t++) {
        for (var k = 0; k < 4; k++) {
            em.setProperty("stage6_" + stg6_combo[t] + "" + (k + 1) + "", "0")
        }
    }
    for (var t = 0; t < stg6_combo.length; t++) {
        var m = false;
        while (!m) {
            for (var l = 0; l < 4; l++) {
                if (!m) {
                    var d = false;
                    for (var j = 0; j < 4; j++) {
                        if (em.getProperty("stage6_" + stg6_combo[t] + "" + (j + 1) + "") == null) {
                            em.setProperty("stage6_" + stg6_combo[t] + "" + (j + 1) + "", "0")
                        } else {
                            if (em.getProperty("stage6_" + stg6_combo[t] + "" + (j + 1) + "").equals("1")) {
                                d = true;
                                break
                            }
                        }
                    }
                    if (!d && java.lang.Math.random() < 0.25) {
                        em.setProperty("stage6_" + stg6_combo[t] + "" + (l + 1) + "", "1");
                        m = true;
                        break
                    }
                }
            }
        }
    }
    for (var o = 0; o < 3; o++) {
        em.setProperty("stage62_" + o + "", "0")
    }
    var f = false;
    while (!f) {
        for (var o = 0; o < 3; o++) {
            if (em.getProperty("stage62_" + o + "") == null) {
                em.setProperty("stage62_" + o + "", "0")
            } else {
                if (!f && java.lang.Math.random() < 0.2) {
                    em.setProperty("stage62_" + o + "", "1");
                    f = true
                }
            }
        }
    }
    var e = false;
    while (!e) {
        for (var o = 0; o < 3; o++) {
            if (em.getProperty("stage62_" + o + "") == null) {
                em.setProperty("stage62_" + o + "", "0")
            } else {
                if (!em.getProperty("stage62_" + o + "").equals("1") && !e && java.lang.Math.random() < 0.2) {
                    em.setProperty("stage62_" + o + "", "1");
                    e = true
                }
            }
        }
    }
    return g
}

function onItemPickedUp(h, j, i) {
    var c = p.getMapId();
    var d = 0;
    var b = 0;
    var f = "";
    switch (c) {
        case 920010000:
            f = "雅典娜禁地<岔路>";
            d = 20;
            b = 920010000;
            break;
        case 920010200:
            f = "雅典娜禁地<散步路>";
            d = 12;
            b = 0;
            break;
        case 920010900:
        case 920010910:
        case 920010912:
        case 920010920:
        case 920010922:
        case 920010930:
        case 920010932:
            f = "雅典娜禁地<罪人之室>";
            d = 3;
            b = 0;
            break;
        default:
            break
    }
    if (d > 0) {
        if (i == 4001063) {
            var e = 0;
            h.getPlayers().forEach(function (l) {
                e += l.getAPI().getItemQuantity(4001063)
            });
            var k = d - e;
            var g = j.getAPI().getJobName(j.getAPI().getJob());
            var a = j.getName();
            if (e >= d) {
                h.getPlayers().forEach(function (m) {
                    m.getAPI().fieldEffect_ScreenMsg("quest/party/clear");
                    m.getAPI().fieldEffect_PlayFieldSound("Party1/Clear", 100);
                    m.getAPI().fieldEffect_SetObjectState(100, "gate");
                    m.getAPI().removeAll(4001063);
                    for (var l = 4001044; l < 4001064; l++) {
                        m.getAPI().removeAll(l)
                    }
                    m.getAPI().playerMessage(-1, "通往下一关的传送口打开了。");
                    WarpTask(b, 2)
                });
                em.setProperty("pre", "1")
            } else {
                h.getPlayers().forEach(function (l) {
                    l.getAPI().getTopMsgFont("" + g + "<" + a + ">在『" + f + "』找到第" + (e) + "个云片。", 3, 20, 13, 0);
                    l.getAPI().showMapEffect(true, 5120018, "还差" + k + "枚通关。", 15, false)
                })
            }
        }
        if (i == 4001050) {
            var e = 0;
            h.getPlayers().forEach(function (l) {
                e += l.getAPI().getItemQuantity(4001050)
            });
            var k = d - e;
            var g = j.getAPI().getJobName(j.getAPI().getJob());
            var a = j.getName();
            h.getPlayers().forEach(function (l) {
                if (e >= d) {
                    l.getAPI().fieldEffect_ScreenMsg("quest/party/clear");
                    l.getAPI().fieldEffect_PlayFieldSound("Party1/Clear", 100);
                    l.getAPI().fieldEffect_SetObjectState(100, "gate");
                    l.getAPI().playerMessage(-1, "搜集完毕去找NPC谈谈吧。")
                } else {
                    l.getAPI().getTopMsgFont("" + g + "<" + a + ">在『" + f + "』找到第" + (e) + "个女神像碎片1。", 3, 20, 13, 0);
                    l.getAPI().showMapEffect(true, 5120018, "还差" + k + "枚通关。", 15, false)
                }
            })
        }
        if (i == 4001158) {
            var e = 0;
            h.getPlayers().forEach(function (l) {
                e += l.getAPI().getItemQuantity(4001158)
            });
            var k = d - e;
            var g = j.getAPI().getJobName(j.getAPI().getJob());
            var a = j.getName();
            if (e >= d) {
                h.getPlayers().forEach(function (l) {
                    l.getAPI().fieldEffect_ScreenMsg("quest/party/clear");
                    l.getAPI().fieldEffect_PlayFieldSound("Party1/Clear", 100);
                    l.getAPI().fieldEffect_SetObjectState(100, "gate");
                    l.getAPI().addPopupSay(2013001, 4000, "搜集完毕，队长去找NPC谈谈吧。")
                });
                em.setProperty("feather_end", "1")
            } else {
                h.getPlayers().forEach(function (l) {
                    l.getAPI().getTopMsgFont("" + g + "<" + a + ">在『" + f + "』找到第" + (e) + "个羽毛。", 3, 20, 13, 0);
                    l.getAPI().showMapEffect(true, 5120018, "还差" + k + "个通关。", 15, false)
                })
            }
        }
    }
}

function WarpTask(b, a) {
    player.scheduleWarpTask(4, b, a, false)
}

function onPlayerRegistered(b, a) {
    var c = b.getMapInstance(实例地图[0]);
    a.dropMessage(6, "[" + 副本名称 + "] 进入到了挑战地图，请小心行事。");
    a.changeMap(c, c.getPortal(0));
    a.setReviveCount(复活次数);
    a.getAPI().setNumberForQuestInfo(准备地图, "damage", 0);
    a.tryPartyQuest(1203)
}

function onPlayerKilled(b, a) {}

function onPlayerRevived(b, a) {
    if (a.getReviveCount() > 0) {
        return true
    } else {
        var c = b.getMapFactoryMap(准备地图);
        a.changeMap(c, c.getPortal(0));
        return true
    }
}

function onMapChanged(d, c, b) {
    var a = 实例地图.indexOf(b);
    if (a < 0) {
        on玩家退场(d, c, false);
        return
    }
    switch (a) {
        case 0:
            break;
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            break;
        case 8:
            break;
        case 9:
            break;
        case 10:
            break;
        case 11:
            break;
        case 12:
            break;
        case 13:
            break;
        case 14:
            break;
        default:
            if (b == 920011100 && em.getProperty("done").equals("0")) {
                em.setProperty("done", "1");
                d.restartEventTimer(60000)
            } else {
                d.startEventTimer(退场倒计时 * 1000)
            }
            break
    }
}

function 远古精灵(c, b, a, e) {
    var d = eim.getMapInstance(实例地图[c]);
    mobId = 9300039;
    mob = em.getMonster(mobId);
    mob.setOverrideStats();
    mob.getChangedStats().hp = 999999999;
    mob.setSize(b);
    mob.getChangedStats().matk = 50;
    mob.getChangedStats().watk = 50;
    mob.getChangedStats().acc = 9999999;
    mob.getChangedStats().level = 150;
    mob.heal();
    eim.registerMonster(mob);
    d.spawnMonsterOnGroundBelow(mob, new java.awt.Point(a, e))
}

function onMonsterKilled(c, e) {
    if (e == 9300049 && em.getProperty("stage7").equals("0")) {
        c.broadcastPlayerMsg(6, "远古精灵出现了！");
        远古精灵(13, 300, -830, 563);
        em.setProperty("stage7", "1")
    } else {
        if (e == 9300040) {
            var a = em.getNumberProperty("stage2");
            if (a < 14) {
                c.broadcastPlayerMsg(6, "它已经在地图的某个地方诞生了。");
                var d = em.getMonster(9300040);
                em.setProperty("stage2", a + 1);
                c.registerMonster(d);
                c.getMapInstance(3).spawnMonsterOnGroundBelow(d, new java.awt.Point(cx[a], cy[a]))
            }
        } else {
            if (e == 9300039) {
                openNpc(c, 9130027, "评价系统");
                for (var b = 0; b < c.getPlayerCount(); b++) {
                    c.getPlayers().get(b).dropMessage(6, "[" + 副本名称 + "] 成功击杀BOSS！")
                }
            }
        }
    }
    return 1
}

function onMonsterDamaged(b, a, d, e, c) {
    if (d == BOSS_1_id) {
        a.getAPI().addNumberForQuestInfo(准备地图, "damage", e)
    }
}

function openNpc(c, b, d) {
    for (var a = 0; a < c.getPlayerCount(); a++) {
        c.getPlayers().get(a).getAPI().openNpc(b, d)
    }
}

function randomNum(b, a) {
    switch (arguments.length) {
        case 1:
            return parseInt(Math.random() * b + 1, 10);
            break;
        case 2:
            return parseInt(Math.random() * (a - b + 1) + b, 10);
            break;
        default:
            return 0;
            break
    }
}

function initProp(a, b) {
    if (em.getProperty(a) == null) {
        em.setProperty(a, b)
    }
}

function initPropArray(a, d, c, e) {
    for (var b = c; b <= e; b++) {
        if (em.getProperty(a + "_" + b) == null) {
            em.setProperty(a + "_" + b, d)
        }
    }
}

function initPropArrayRandom(a, d, j, b, h) {
    var g = new Array();
    var k = h - b + 1;
    var c = (j - d) < (h - b) ? true : false;
    do {
        var f = randomNum(d, j);
        if (c || g.indexOf(f) < 0) {
            g.push(f);
            k--
        }
    } while (k);
    for (var e = b; e <= h; e++) {
        if (em.getProperty(a + "_" + e) == null) {
            em.setProperty(a + "_" + e, g[e - 1])
        }
    }
}

function on玩家退场(c, b, a) {
    c.disposeIfPlayerBelow(a ? 999 : 0, 准备地图);
    em.getProperties().clear();
    em.setProperty("state", 0);
    em.setProperty("leader", "true");
    c.unregisterPlayer(b)
}

function onPartyDisbanded(a) {
    a.getPlayers().forEach(function (b) {
        on玩家退场(a, b, true)
    })
}

function onTimeOut(a) {
    a.getPlayers().forEach(function (b) {
        on玩家退场(a, b, true)
    })
}

function onPlayerDisconnected(b, a) {
    on玩家退场(b, a, true);
    return 0
};